package com.stox.scanner.ui.swing.view;

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.util.Collections;
import java.util.List;

import javax.swing.JScrollPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import com.stox.core.ui.swing.wrapper.JTree;
import com.stox.ioc.Container;
import com.stox.navigator.ui.swing.view.AbstractNavigatorTab;
import com.stox.resources.i18n.MessageSource;
import com.stox.scanner.model.Screen;

public class ScreensPanel  extends AbstractNavigatorTab{
	private static final long serialVersionUID = 315320138859466287L;

	private JTree tree;
	
	@SuppressWarnings("unchecked")
	@Override
	public void initializeInternal() {
		DefaultMutableTreeNode root = new DefaultMutableTreeNode(MessageSource.getMessage("Screens"));
		List<? extends Screen<?>> screens = (List<? extends Screen<?>>)Container.getImplementations(Screen.class);
		Collections.sort(screens);
		for(Screen<?> screen : screens){
			DefaultMutableTreeNode node = new DefaultMutableTreeNode(screen);
			root.add(node);
		}
		tree = new JTree(root);
		DefaultMutableTreeNode targetNode = (DefaultMutableTreeNode)root.children().nextElement();
		TreePath path = new TreePath(((DefaultTreeModel)tree.getModel()).getPathToRoot(targetNode));
		tree.expandPath(path);
		tree.scrollPathToVisible(path);
		tree.setRootVisible(false);
		add(new JScrollPane(tree), BorderLayout.CENTER);
	}
	
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	

	@Override
	public int getOrder() {
		return 1;
	}
	
	@Override
	public String getName() {
		return MessageSource.getMessage("Screens");
	}
	
	@Override
	public JTree get() {
		return tree;
	}

}
